package cn.ekuma.epos.datalogic.define.dao.erp;

import java.util.List;

import com.openbravo.bean.erp.viewbean.ProductCategoryAnalysisInfo;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.QBFBuilder;
import com.openbravo.data.loader.SerializerWriteBasic;
import com.openbravo.data.loader.StaticSentence;
import cn.ekuma.data.dao.ViewDAO;

public class ProductCategoryAnalysisInfoDAO extends
		ViewDAO<ProductCategoryAnalysisInfo> {

	public ProductCategoryAnalysisInfoDAO(I_Session s) {
		super(s);
	}

	@Override
	public ProductCategoryAnalysisInfo readValues(DataRead dr,ProductCategoryAnalysisInfo obj)
			throws BasicException {
		if(obj==null)
			obj=new ProductCategoryAnalysisInfo();
		obj.setID(dr.getString(1));
		obj.setName(dr.getString(2));
		obj.setM_sParentID(dr.getString(3));
		obj.setMultiply(dr.getDouble(4));
		obj.setBuyPrice(dr.getDouble(5));
		obj.setSellPrice(dr.getDouble(6));
		obj.setProfits(new Double(obj.getSellPrice() / obj.getBuyPrice() - 1.0));
		return obj;
	}

	@Override
	public Class getSuportClass() {	
		return ProductCategoryAnalysisInfo.class;
	}

	@Override
	public List<ProductCategoryAnalysisInfo> list(Object filter)
			throws BasicException {
		return new StaticSentence(s, new QBFBuilder(
                "SELECT C.ID,C.NAME,C.PARENTID,SUM(S.UNITS),SUM(S.UNITS*P.PRICEBUY),SUM(S.UNITS*P.PRICESELL) "
                + "FROM STOCKCURRENT S "
                + "LEFT JOIN PRODUCTS P ON S.PRODUCT = P.ID "
                + "LEFT JOIN CATEGORIES C ON P.CATEGORY = C.ID "
                + "LEFT OUTER JOIN OPERATIONPRODUCT OP ON S.PRODUCT = OP.PRODUCT  "
                + "LEFT OUTER JOIN OPERATIONPRODUCTCATEGORIES OPC ON OP.OPERATIONCATEGORIE = OPC.ID  "
                + "WHERE ?(QBF_FILTER) "
                + "GROUP BY C.ID,C.NAME,C.PARENTID ",
                new String[]{"S.LOCATION","OPC.ID"}), new SerializerWriteBasic(new Datas[]{Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.STRING}), this).list(filter);

	}

	public List<ProductCategoryAnalysisInfo> listPosCategoryAnalysisInfo(Object filter)
			throws BasicException {
		return new StaticSentence(s, new QBFBuilder(
                "SELECT C.ID,C.NAME,C.PARENTID,SUM(L.UNITS),SUM(L.UNITS*L.CONSULTCOST),SUM(L.UNITS*L.PRICE) "
                + "FROM TICKETLINES L LEFT JOIN TICKETS T ON L.TICKET = T.ID "
                + "LEFT JOIN PRODUCTS P ON L.PRODUCT = P.ID "
                + "LEFT JOIN CATEGORIES C ON P.CATEGORY = C.ID "
                + "LEFT OUTER JOIN OPERATIONPRODUCT OP ON L.PRODUCT = OP.PRODUCT  "
                + "LEFT OUTER JOIN OPERATIONPRODUCTCATEGORIES OPC ON OP.OPERATIONCATEGORIE = OPC.ID  "
                + "WHERE L.PRODUCT IS NOT NULL AND ?(QBF_FILTER) "
                + "GROUP BY C.ID,C.NAME,C.PARENTID ",
                new String[]{"T.CURDATE", "T.CURDATE", "T.CUSTOMER", "T.LOCATIONREF","OPC.ID"}), new SerializerWriteBasic(new Datas[]{Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.OBJECTS, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.STRING}), this).list(filter);

	}
	
		
}
